//index.js
//获取应用实例
const app = getApp()
const WxParse = require('../../wxParse/wxParse.js')
import {
  getCarousel,
  getPageData
} from '../../api/carousel'
import {
  getGoods
} from '../../api/goods'
import {
  getKanJiaGoods
} from '../../api/kanJia'
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 6000,
    duration: 800,
    swiperCurrent: 0,
    iphone: false,
    loadingHidden: false, // loading
    wxlogin: true,
    videoSrc: '',
    loadingMoreHidden: true,
    showSearch: true,
    showModal: false,
    fxGoods: [{
      Thumb: 'https://mallcdn.oss-cn-beijing.aliyuncs.com/15618701490029568.jpg',
      Name: '祥康酒业500ml祥康酒',
      DefaultPrice: '竞拍获得',
      // ViewCount: ''
    }, {
      Thumb: 'https://mallcdn.oss-cn-beijing.aliyuncs.com/15557686555984896.png',
      Name: '祥康酒业750ml祥康酒',
      DefaultPrice: '竞拍获得',
      // ViewCount: ''
    }],
    sharePic: '',
    text: "",
    animation: null,
    timer: null,
    textWidth: 0,
    wrapWidth: 0,
    lilPic: '',
    reBuyGoods: [],
    statusBarHeight: 0,
    titleBarHeight: 0,
    imgList: {},
    topBanners: [],
    bottomBanners: [],
    goodsList: [],
    huoDongList: [{
      url: 'https://mallcdn.oss-cn-beijing.aliyuncs.com/15986945137148928.webp',
      tap: 'panicBuy',
      show: true
    },
    {
      url: 'https://mallcdn.oss-cn-beijing.aliyuncs.com/15989975063692288.png',
      tap: 'kanJia',
      show: true
    }]
  },
  async getData() {
    // let goodsList = await getGoods(1, 99999,'')
    let topBanner = await getCarousel('TOP')
    this.setData({
      topBanners: topBanner.data
    })
    let bottomBanner = await getCarousel('BOTTOM')
    this.setData({
      bottomBanners: bottomBanner.data
    })
    let goodsData = await getGoods(1, 100, '', true)
    this.setData({
      goodsList: goodsData.data.records
    })
    let pageData = await getPageData('INTRODUCTION')
    this.setData({
      pageData: pageData.data
    })
  },
  videometa: function (e) {
    var that = this;
    //获取系统信息
    wx.getSystemInfo({
      success(res) {
        //视频的高
        var height = e.detail.height;

        //视频的宽
        var width = e.detail.width;

        //算出视频的比例
        var proportion = height / width;

        //res.windowWidth为手机屏幕的宽。
        var windowWidth = res.windowWidth;

        //算出当前宽度下高度的数值
        height = proportion * windowWidth;
        that.setData({
          height,
          width: windowWidth
        });
      }
    })
  },
  cancelLogin() {
    this.setData({
      showModal: false
    })
  },
  notLogin() {
    this.setData({
      showModal: true
    })
  },
  login() {
    let that = this
  },
  async onShow() {
    var that = this
    this.setData({
      imgList: app.globalData.imgList
    })
    this.getData()
    // if (app.globalData.shopId) {
    //   setTimeout(function () {
    //     if (app.globalData.usinfo == 0) {
    //       that.setData({
    //         wxlogin: false
    //       })
    //       wx.hideTabBar();
    //     }
    //   }, 800)

    // } else {
    //   setTimeout(this.onShow, 300);
    // }
  },
  onShareTimeline: function () {
    return {
      title: '祥康酱酒',
      path: `/pages/index/index?id=${app.globalData.uid}`,
      imageUrl: 'https://mallcdn.oss-cn-beijing.aliyuncs.com/15920383876203520.jpg'
    }
  },
  onShareAppMessage() {
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline']
    })
    return {
      title: '祥康酱酒',
      path: `/pages/index/index?id=${app.globalData.uid}`,
      imageUrl: 'https://mallcdn.oss-cn-beijing.aliyuncs.com/15920383876203520.jpg'
    }
  },
  onLoad: function (e) {
    // console.log(e)
    //分享进入时上级Id 是否存在
    this.ready()
    app.fadeInOut(this, 'fadeAni2', 0)
    app.fadeInOut(this, 'fadeAni1', 1)
    if(e.id){
      wx.setStorageSync('fromId', e.id)
    }
    // if (app.globalData.shopId) {
    //   if (e.id) {
    //     //上级Id是否等于自己的Id
    //     if (e.id != app.globalData.uid) {
    //       //分享的规格Id 是否存在
    //       if (e.standId) {
    //         //存储缓存 在申请经销商时判断，并取规格Id购买
    //         wx.setStorageSync('standId', e.standId)
    //       } else {
    //         wx.setStorageSync('standId', null)
    //       }
    //       // app.get(`dealer/get_dealer_fromid?CustId=${e.id}`, rep => {
    //       //   // console.log(rep)
    //       //   if (rep.code == 0) {
    //       //     wx.setStorageSync('dealerInfo', rep.result)
    //       //   } else {
    //       //     wx.setStorageSync('dealerInfo', null)
    //       //   }
    //       // })
    //     }
    //   } else {}
    //   var that = this
    //   if (app.globalData.iphone == true) {
    //     that.setData({
    //       iphone: true
    //     })
    //   }
    // } else {
    //   // setTimeout(()=>this.onLoad(e),300);
    // }
  },
  panicBuy(){
    if (!wx.getStorageSync('access_token')) {
      wx.showModal({
        title: '请先登录',
        content: '',
        showCancel: false,
        success: (res) => {
          wx.reLaunch({
            url: '../my/my' //返回上一级页面
          })
        }
      })
      return
    }
    wx.navigateTo({
      url: '../panicBuy/panicBuy',
    })
  },
  kanJia(){
    if (!wx.getStorageSync('access_token')) {
      wx.showModal({
        title: '请先登录',
        content: '',
        showCancel: false,
        success: (res) => {
          wx.reLaunch({
            url: '../my/my' //返回上一级页面
          })
        }
      })
      return
    }
    wx.navigateTo({
      url: '../kanJia/kanJiaList',
    })
  },
  ready: function () {
    // 因为很多地方都需要用到，所有保存到全局对象中
    if (app.globalData && app.globalData.statusBarHeight && app.globalData.titleBarHeight) {
      this.setData({
        statusBarHeight: app.globalData.statusBarHeight,
        titleBarHeight: app.globalData.titleBarHeight
      });
    } else {
      let that = this
      wx.getSystemInfo({
        success: function (res) {
          if (!app.globalData) {
            app.globalData = {}
          }
          if (res.model.indexOf('iPhone') !== -1) {
            app.globalData.titleBarHeight = 44
          } else {
            app.globalData.titleBarHeight = 48
          }
          app.globalData.statusBarHeight = res.statusBarHeight
          that.setData({
            statusBarHeight: app.globalData.statusBarHeight,
            titleBarHeight: app.globalData.titleBarHeight
          });
        },
        failure() {
          that.setData({
            statusBarHeight: 0,
            titleBarHeight: 0
          });
        }
      })
    }
  },
  goDetail(e) {
    let item = new Object;
    item.IsBuy = e.currentTarget.dataset.dealer;
    item.Id = e.currentTarget.dataset.id;
    let url = `/pages/goods-detail/goods-detail?isDealer=${item.IsBuy}&id=${item.Id}`
    wx.navigateTo({
      url: `/pages/goods-detail/goods-detail?isDealer=${item.IsBuy}&id=${item.Id}`,
    })
  },
  swiperchange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-detail/goods-detail?id=" + e.currentTarget.dataset.id
    })
  },
  tapBanner: function (e) {
    let type = e.currentTarget.dataset.type
    if (type == 1) {
      // 文章
    } else if (type == 2) {
      // 商品
      wx.navigateTo({
        url: "/pages/goods-detail/goods-detail?id=" + e.currentTarget.dataset.id
      })
    }
  },
  tapSales: function (e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: e.currentTarget.dataset.id
      })
    }
  },
  userlogin: function (e) {
    var that = this;
    var iv = e.detail.iv;
    var encryptedData = e.detail.encryptedData;
    wx.login({
      success: function (wxs) {
        wx.request({
          url: app.globalData.urls + '/user/wxapp/register/complex',
          data: {
            code: wxs.code,
            encryptedData: encryptedData,
            iv: iv
          },
          success: function (res) {
            if (res.data.code != 0) {
              wx.showModal({
                title: '温馨提示',
                content: '需要您的授权，才能正常使用哦～',
                showCancel: false,
                success: function (res) {}
              })
            } else {
              that.setData({
                wxlogin: true
              })
              app.login();
              wx.showToast({
                title: '授权成功',
                duration: 2000
              })
              app.globalData.usinfo = 1;
              wx.showTabBar();
            }
          }
        })
      }
    })
  },
  onPageScroll: function (t) {
    if (t.scrollTop >= 360) {
      app.fadeInOut(this, 'fadeAni2', 1)
      app.fadeInOut(this, 'fadeAni1', 0)
    } else {
      app.fadeInOut(this, 'fadeAni2', 0)
      app.fadeInOut(this, 'fadeAni1', 1)
    }
  },
  onHide() {
    this.destroyTimer()
    this.setData({
      timer: null
    })
  },
  onUnload() {
    this.destroyTimer()
    this.setData({
      timer: null
    })
  },
  destroyTimer() {
    if (this.data.timer) {
      clearTimeout(this.data.timer);
    }
  },
})